package top.canyue.eca_manager.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import top.canyue.eca_manager.bean.OperationLog;

import java.util.List;
import java.util.Map;

@Mapper
public interface OperationLogMapper {

    @Insert("INSERT INTO operationlog(id, user_id, level, operation, params, result_static, create_time) VALUES " +
            "( MD5(UUID()), #{userId}, #{level}, #{operation}, #{params}, #{resultStatic}, NOW() )")
    Integer record(OperationLog operationLog);

    @Select("SELECT date_format(create_time,'%Y-%m-%d') AS `date`, COUNT(1) AS `count`" +
            "FROM operationlog " +
            "WHERE `level` = 'INFO' AND operation = 'login' " +
            "AND DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(create_time) " +
            "GROUP BY date ORDER BY date ")
    List<Map<String, Object>> getLoginTrendByWeek();

    @Select("SELECT level,count(1) as `count` FROM operationlog " +
            "GROUP BY level " +
            "LIMIT 100")
    List<Map<String, Object>> getSystemStability();


}
